﻿@{
    ViewBag.Title = "User Level Rights";
    Layout = "~/Views/Shared/_Layout.cshtml";

    var userLevels = ViewBag.userLevels as IEnumerable<BillBox.Models.UserLevel>;
    int levelId = ViewBag.levelId;
    string errorMessage = ViewBag.errorMessage;
}


<div class="row main">
    <div class="col-lg-12">
        <ol class="breadcrumb">
            <li>@Html.ActionLink("Home", "Index", "Default")</li>
            <li>@Html.ActionLink("Settings", "Index", "Settings")</li>
            <li class="active">Permissions</li>
        </ol>
        <!--<h1 class="page-header">Add Capture Field</h1>-->
        <div class="panel panel-default">
            <div class="panel-heading">
                @if (errorMessage != null && errorMessage != string.Empty)
                {
                    <span>@errorMessage</span>
                }
                else
                {
                    <span>Permissions</span>
                }
            </div>
            <div class="panel-body">
                <div class="row">
                    <div class="col-lg-6">
                        @using (Html.BeginForm("UserLevelRights", "Settings", FormMethod.Post, new { role = "form", @class = "form-horizontal" }))
                        {
                            @Html.AntiForgeryToken()
                            @Html.ValidationSummary(true)

                            <div class="form-group">
                                @Html.Label("User Level", new { @class = "col-sm-5 control-label" })
                                <div class="col-sm-7">
                                    @Html.DropDownList("LevelId", new SelectList(userLevels, "LevelId", "LevelName", levelId), new { @class = "form-control" })
                                </div>
                            </div>
                            <div id="rights-area"></div>

                            <div class="form-group">
                                <div class="col-sm-offset-5 col-sm-7">
                                    <button type="submit" class="btn btn-default">Save</button>
                                </div>
                            </div>
                        }
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

@section Scripts {
    <script type="text/javascript">
        $(document).ready(function () {

            loadRights($("#LevelId").val());

            $("#LevelId").change(function (e) {
                loadRights($(this).val());
            });

            function loadRights(levelId) {
                if (levelId) {
                    $.ajax({
                        cache: false,
                        type: 'GET',
                        url: '@Url.Action("UserLevelRightsFields", "Settings")',
                        data: { 'levelId': levelId },
                        success: function (html) {
                            $("#rights-area").html(html);
                        },
                        error: function () {
                            $("#rights-area").html("");
                        }
                    });
                }
            }
        });
    </script>
}
